
import {
  productList,
  allNetwork,
  systematicsList,
  merchantUpdate
} from '../../api.js'
let app = getApp();
var startPoint;
Page({
  data: {
    full_name:"",
    token:"",
    sortid:0,
    shopid:0,
    sort:[{
      id:0,
      tit: "新上架优先",
      val:1
    },
    {
      id:2,
        tit:"价格低到高",
        val:2
    },{
      id:3,
        tit:"价格高到低",
        val:1
    }],
    internet: true,
    load: false,
    lasttit: "",
    inputtext: "",
    checkControl: 'allGoodControl', // 0  商品库存；  1 
    productItems: [], //商品库存所有商品
    hasProduct: true, //默认不显示空白页面
    selected1: 0, //商品库存筛选
    productCate: [
      {
        id:0,
        initials:"A",
        title:"全部"
      }
    ], //商品类别
    allProductItems: [], //全网库存商品
    category:[
      [],[],[],[]
    ],
    hasAllProduct: true, //默认不显示空白页面
    changname: true,
    sortshop:true,//筛选排序
    currpage:1,
    lastpage:2,
    peer_price:"",
    category_id:0,
    time_sort:""
  },
  //筛选排序
  search(){
    wx.navigateTo({
      url: 'search/search',
    })
  },
  sort(e){
    this.setData({
      sortid: e.currentTarget.dataset.id,
      sortshop:true
    })
    if (e.currentTarget.dataset.id == 2 || e.currentTarget.dataset.id == 3){
      if (this.data.shopid != 0){
        this.AllShopList({
          peer_price: e.currentTarget.dataset.val,
          category_id: this.data.shopid
        })
        this.setData({
          peer_price: e.currentTarget.dataset.val,
          category_id: this.data.shopid
        })
      }else{
        this.AllShopList({
          peer_price: e.currentTarget.dataset.val
        })
        this.setData({
          peer_price: e.currentTarget.dataset.val
        })
      }
    }else if(e.currentTarget.dataset.id == 0){
      if (this.data.shopid != 0) {
        this.AllShopList({
          category_id: this.data.shopid,
          time_sort: this.data.shopid
        })
        this.setData({
          category_id: this.data.shopid,
          time_sort: this.data.shopid
        })
      } else {
        this.AllShopList({
          time_sort: e.currentTarget.dataset.val
        })
        this.setData({
          time_sort: e.currentTarget.dataset.val
        })
      }
    }
  },
  // 关掉筛选框
  closes(){
    console.log("1")
    this.setData({
      sortshop: true
    })
  },
  // 打开排序
  opensort(){
    this.setData({
      sortshop: !this.data.sortshop
    })
  },
  // 选择类型
  tabs(e){
    var tab = e.currentTarget.dataset.id;
    
    if(tab == 0) {
      this.AllShopList()
    } else {
      this.AllShopList({
        category_id: tab
      })
    }
    this.setData({
      shopid: e.currentTarget.dataset.id,
      category_id: this.data.shopid,
      allProductItems:[]
    })
   
  },
  AllShopList(obj){
    allNetwork({
        token: this.data.token,
      ...obj
    }).then(res => {
      if (res.data.code === 666) {
        if (res.data.data.current_page == 1) {
          this.setData({
            allProductItems: res.data.data.data
          })
        } else {
          this.setData({
            allProductItems: this.data.allProductItems.concat(res.data.data.data),
          })
        }
        this.setData({
          internet: true,
          load: true,
          currpage: res.data.data.current_page,
          lastpage: res.data.data.last_page,
          lasttit: ""
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        this.setData({
          lasttit: "暂无更多数据"
        })
      }
    })
  },
  onLoad: function (options) {
    this.setData({
      token:app.globalData.token
    })
    if (app.globalData.is_name_updated == 0) {
      this.setData({
        changname: false
      })
    }
    var _this = this;
    setTimeout(() => {
      if (this.data.load) {
      
      } else {
        this.setData({
          internet: false,
          load: true
        })
      }
    }, 6000)
    systematicsList({
      token: app.globalData.token
    }).then(res => {
      res.data.data.forEach(r=>{
        r.bol=false
      })
      if (res.data.code == 666) {
        console.log(res.data.data)
        _this.setData({
          productCate: _this.data.productCate.concat(res.data.data)
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
    this.AllShopList()
  },
  /* 生命周期函数--监听页面显示*/
  onShow: function () {
    this.setData({
      sortshop:true
    })
  },
  // 添加商品名称
  input(e) {
    this.setData({
      inputtext: e.detail.value
    })
  },
  changename() {
    if (this.data.inputtext == "") {
      wx.showToast({
        title: '请输入真实商铺名称再提交',
        icon: "none"
      })
      return
    }
    this.setData({
      changname: true
    })
    merchantUpdate({
      token: app.globalData.token,
      shopname: this.data.inputtext
    }).then(res => {
      if (res.data.code == 666) {
        wx.showToast({
          title: '商品名修改成功',
        })
      }
    })
  },
  onHide: function () {
    this.setData({
      lasttit: ""
    })
    wx.setStorageSync('controlType', '');
    wx.setStorageSync('del-add-data', null)
  },
  close() {
    this.setData({
      changname: true
    })
  },
  toBuy(e) {
    let id = e.currentTarget.dataset.id;
    let price = e.currentTarget.dataset.price;
    let picture_list = e.currentTarget.dataset.picture;
    let deliver = e.currentTarget.dataset.deliver;
    let full_name = e.currentTarget.dataset.fullName.replace(/\&/g, '@');
    let is_synchronize = e.currentTarget.dataset.is_synchronize
    wx.navigateTo({
      url: '/pages/goodControl/toBuy/toBuy?id=' + id + '&is_synchronize=' + is_synchronize,
    })
  },
  onPullDownRefresh(){
    wx.stopPullDownRefresh()
  },
  

  /*页面上拉触底事件的处理函数*/
  onReachBottom: function () {
    if(this.data.currpage>this.data.lastpage){
      return
    }else{
      this.setData({
        lasttit: "正在加载"
      })
      var page=this.data.currpage+1
      var category_id = this.data.shopid
      var peer_price = this.data.peer_price
      var time_sort = this.data.time_sort
      this.AllShopList({
        page,
        category_id,
        peer_price,
        time_sort
      })
    }
  },
  onShareAppMessage: function (res) {
    var that = this;
    var goods_title = '赋能奢商，只为业绩增长';//获取产品标题
    var goods_img = "https://document.growthupup.com/growthShare.jpg?t=" + new Date().getTime()
    return {
      title: goods_title,
      path: '/pages/loading/loading',
      imageUrl: goods_img //不设置则默认为当前页面的截图
    }
  }
})
